(function ($) {
    var AdminForm = function () {
        this.totalImages = 0;
        this.pageCount = 0;
        this.heightList = 0;
        this.userExistCompetition = false;
        this.currentStep = "home";
    };

    AdminForm.prototype.initAdminForm = function () {
    	var self = this;
        $('.table').tableScroll({height:500}); 
        if($('.table-total').length && $('.cat_id').val()==1){
           self.initCheckTotal('');
        }
    }
    AdminForm.prototype.slideToggle = function(e) {
    	$(e).slideToggle();
    }
    
    AdminForm.prototype.checkBroswer = function() {
        if($.browser.msie && parseInt($.browser.version) <= 6) {
            return true;
        }
            return false;
    }
    
    AdminForm.prototype.checkCategory = function(eleval) {
        if(eleval==2)
        {
            $('.hidden-tr td').css('padding', '10px 5px');
            $('.print-hidden').css('display','block');
            $('.print-select').css('display','block');
            if($('.print-select').val()==1 || $('.print-select').val()==2)
            {
                $('.ref-no-tr td').css('padding', '10px 5px');
                $('.ref-no').css('display','block');
                /*var refVal = $('.ref-no').val(); 
                if($('.print-select').val()==2)
                {
                    $('.ref-no').val($(this).attr('data-dv'));
                }*/
            }
            else {
                if($('.print-select').val()==4)
                {
                    $('.hidden-contract-tr td').css('padding', '10px 5px');
                    $('.contract-hidden').css('display','block');
                }else
                {
                    $('.hidden-contract-tr td').css('padding', 0);
                    $('.contract-hidden').css('display','none');
                }
                $('.ref-no-tr td').css('padding', 0);
                $('.ref-no').css('display','none');
            }
        }
        else{
            $('.hidden-tr td').css('padding',0);
            $('.print-hidden').css('display','none');
            $('.print-select').css('display','none');
            $('.hidden-contract-tr td').css('padding',0);
            $('.contract-hidden').css('display','none');
            $('.ref-no-tr td').css('padding', 0);
            $('.ref-no').css('display','none');  
        }
    }
    
    AdminForm.prototype.checkStatus = function(eleval) {
        var self = this;
        if(eleval==5)
        {
            $('.hidden-return-tr td').css('padding', '10px 5px');
            $('.return-hidden').css('display','block');
            self.initCheckReturn();
        }else{
            $('.hidden-return-tr td').css('padding',0);
            $('.return-hidden').css('display','none');
        }
            
        if(eleval==2 || eleval==4)
        {
            $('.hidden-branch-tr td').css('padding', '10px 5px');
            $('.branch-hidden').css('display','block');
            $('.hidden-broken-tr td').css('padding',0);
            $('.broken-hidden').css('display','none');
            
        }
        else{
            $('.hidden-branch-tr td').css('padding',0);
            $('.branch-hidden').css('display','none');
            if(eleval ==3)
            {
                /*$('.type-tr td').css('padding', '10px 5px');
                $('.type-tr .broken-hidden').css('display','block');*/
                $('.hidden-broken-tr td').css('padding','10px 5px');
                $('.broken-hidden').css('display','block');
                if($('.broken-type').val() == 1)
                {
                    $('.order-tr td').css('padding', '10px 5px');
                    $('.order-tr .broken-hidden').css('display','block'); 
                    $('.unorder-tr td').css('padding', 0);
                    $('.unorder-tr .broken-hidden').css('display','none');  
                }
                else{
                    $('.order-tr td').css('padding', 0);
                    $('.order-tr .broken-hidden').css('display','none'); 
                    $('.unorder-tr td').css('padding', '10px 5px');
                    $('.unorder-tr .broken-hidden').css('display','block');  
                }
            }
            else{
                $('.hidden-broken-tr td').css('padding',0);
                $('.broken-hidden').css('display','none');
            }
        }   
    }
    
    AdminForm.prototype.checkPrintType = function(eleval) {
        if(eleval ==1){
            $('.refnum').val($('.maxref-or').val());
        }
        else if(eleval ==2)
        {
            $('.refnum').val($('.maxref-dv').val());
        }
        else
        {
            $('.refnum').val('');
        }
    }
    
    AdminForm.prototype.checkBrokenType = function(eleval) {
        if(eleval == 1)
        {
            $('.order-tr td').css('padding', '10px 5px');
            $('.order-tr .broken-hidden').css('display','block'); 
            $('.unorder-tr td').css('padding', 0);
            $('.unorder-tr .broken-hidden').css('display','none');  
        }
        else{
            $('.order-tr td').css('padding', 0);
            $('.order-tr .broken-hidden').css('display','none'); 
            $('.unorder-tr td').css('padding', '10px 5px');
            $('.unorder-tr .broken-hidden').css('display','block');  
        }
    }
    
    AdminForm.prototype.checkLocation = function(eleval){
        if(eleval=='00HO00')
        {
            $('.location-tr').show();
        }else
        {
            $('.location-tr').hide();
        }
    }
    
    AdminForm.prototype.checkItemName = function(eleval){
        $('.item-name').val(eleval);
    }
    
    AdminForm.prototype.initCheckTotal = function(val) {
        var self = this;
        $('.title').val($('.type_id option:selected').text());
        //console.log('abcde');
        $.ajax({
             type: 'POST',
             url: '/index.php/inventory/listtotal', 
             data: 'cat_id='+$('.cat_id').val()+'&type_id='+val,
             success: function(resp) { 
                $('.total').val(resp);
             }
        });
    }
    
    AdminForm.prototype.initCheckReturn = function() {
        var self = this;
        $.ajax({
             type: 'POST',
             url: '/index.php/inventory/listreturn', 
             data: 'item_id='+$('.item_id').val()+'&branch_id='+$('.branch_id').val(),
             success: function(resp) {
                var ls = resp.split(',');
                $('.unitreturn').val(ls[0]);
                $('.refreturn').val(ls[1]);
             }
        });
    }
    
    
    AdminForm.prototype.initCheckHidden = function() {
        var self = this;
        self.checkCategory($(".category-select").val());
        self.checkStatus($(".status-select").val());
        //self.checkPrintType($(".print-type").val());
        self.checkLocation($('#branch_id').val());
        self.checkItemName($('.print-type :selected').text());
    }
    
    jQuery(function ($) {
        var adminForm = new AdminForm();
        adminForm.initAdminForm();
        adminForm.initCheckHidden();
        /*$( ".datepicker" ).datepicker();
        $( ".datepicker" ).css('width','200px');
        
        $('.datepicker').on('changeDate', function(ev){
            $(this).datepicker('hide');
        }); or*/
        $('.datepicker').each(function() {
            var minDate = new Date();
            minDate.setHours(0);
            minDate.setMinutes(0);
            minDate.setSeconds(0,0);
            
            var $picker = $(this);
            $picker.datepicker();
            
            var pickerObject = $picker.data('datepicker');
            
            $picker.on('changeDate', function(ev){
                $picker.datepicker('hide');
            });
            
            $picker.css('width','200px');
        });
        
        $(".datepicker-search").datepicker( {
            format: "mm-yyyy",
            viewMode: "months", 
            minViewMode: "months"
        });
        var $picker = $(".datepicker-search");
        $picker.datepicker();
        var pickerObject = $picker.data('datepicker-search');
        $picker.on('changeDate', function(ev){
            $picker.datepicker('hide');
        });
        $picker.css('width','200px');
        
        
        $("#browser").treeview();
        
        $(".form-validate").validate();
        $.validator.setDefaults({
        	submitHandler: function() 
            { 
                $(".form-validate").unbind();
                $(".form-validate").submit();
            }
        });
       
        $('.link-popup').click(function(e) {
           var url = $(this).parent().attr('href'); 
           windowPopup(url);
           return false;
        });
        $('.link-delete').bind("click", function(e) { 
    		return confirm('Are you sure want to delete this item?');
    	});
         $('.link-move').bind("click", function(e) { 
    		return confirm('Are you sure want to move this item back to stock?');
    	});
         $('.link-accept').bind("click", function(e) { 
    		return confirm('Are you sure want to accept this item?');
    	});
        
        $('.check-all').bind("click", function(e) {
            var selected = this.checked;
            $(':checkbox').each(function () { this.checked = selected; }); 
        });
        
        $(".category-select").bind("change", function(e) {
            e.preventDefault();
            adminForm.checkCategory($(this).val());
            
        });
        $(".print-type").bind("change", function(e) {
            e.preventDefault();
            adminForm.checkPrintType($(this).val());
            adminForm.checkItemName($('.print-type :selected').text());
        });
        $(".status-select").bind("change", function(e) {
            e.preventDefault();
            adminForm.checkStatus($(this).val());
        });
        
        $(".broken-type").bind("change", function(e) {
            e.preventDefault();
            adminForm.checkBrokenType($(this).val());
        });
        
        $("#branch_id").bind("change", function(e) {
            e.preventDefault();
            adminForm.checkLocation($(this).val());
        });
        
        $(".type_id").bind("change", function(e) {
            e.preventDefault();
            adminForm.initCheckTotal($(this).val());
        });
        
        $('.btn-search').bind("click",function(e) {
            if($('#datestart').val()==null || $('#datestart').val()==''){
                alert("Select start month");
                return false;
            }
            if($('#dateend').val()==null || $('#dateend').val()==''){
                alert("Select end month");
                return false;
            }
            return true;
        });
        
        function windowPopup(url)
        {
            window.open(url, null, "width=700, height=500, scrollbars=yes");
            return false;   
        }
        
      });

})(jQuery);